package jh.com.jhappbf.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import jh.com.jhappbf.R;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.utils.JsonHelper;

/**
 * @author Administrator
 */
public class ContentInfo extends Activity implements OnClickListener {
    private ImageView mComeBack;
    private Button mRefresh;
    private TextView mQueueContent;
    private TextView mAssayContent;
    private Handler mHandler;
    private LinearLayout mQueueArea;
    private LinearLayout mAssayArea;
    private ProgressBar mProgressBar;
    private final int MSG = 0x123;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.content_msg_activity);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mHandler = new Handler(getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == MSG) {
                    stateChk();
                    Bundle data = msg.getData();
                    if (data != null) {
                        String queue = data.getString("queue");
                        String assay = data.getString("assay");
                        if (queue != null && queue.length() > 0) {
                            mQueueContent.setText(queue);
                        } else {
                            mQueueContent.setText(getString(R.string.ui_contentInfo_not_data));
                        }

                        if (assay != null && assay.length() > 0) {
                            mAssayContent.setText(assay);
                        } else {
                            mAssayContent.setText(getString(R.string.ui_contentInfo_not_data));
                        }
                    }

                    mHandler.postDelayed(new MyRunnable(), 5000);
                }

            }
        };
        findView();
        mHandler.postDelayed(new MyRunnable(), 1000);
    }

    /**
     * 判断状态
     */
    private void stateChk() {
        int res = mQueueArea.getVisibility();
        if (res == View.GONE) {
            mQueueArea.setVisibility(View.VISIBLE);
        }

        int res1 = mAssayArea.getVisibility();
        if (res1 == View.GONE) {
            mAssayArea.setVisibility(View.VISIBLE);
        }

        int res2 = mProgressBar.getVisibility();
        if (res2 == View.VISIBLE) {
            mProgressBar.setVisibility(View.GONE);
        }
    }

    /**
     * 查找控件
     */
    private void findView() {
        mComeBack = findViewById(R.id.ui_contentInfo_comeback);
        mRefresh = findViewById(R.id.ui_contentInfo_refresh);
        mQueueContent = findViewById(R.id.ui_contentInfo_queue);
        mAssayContent = findViewById(R.id.ui_contentInfo_assay);
        mQueueArea = findViewById(R.id.ui_contentInfo_queue_area);
        mAssayArea = findViewById(R.id.ui_contentInfo_assay_area);
        mProgressBar = findViewById(R.id.ui_contentInfo_progress);
        mComeBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ui_contentInfo_comeback:
                this.finish();
                break;
            default:
                break;
        }
    }

    class MyRunnable implements Runnable {
        @Override
        public void run() {
            ArrayList<String> param = new ArrayList<>();
            ArrayList<String> value = new ArrayList<>();
            String methodName = "getQueueAssayInfo";
            param.add("indentify");
            value.add(State.mAccount);

            Listener<String> listener = response -> {

                JSONObject object = JsonHelper.getJsonObject(response);
                try {
                    String queue = "";
                    String assay = "";
                    if (object != null) {
                        queue = object.getString("queue");
                        assay = object.getString("assay");
                    }

                    Bundle data = new Bundle();
                    data.putString("queue", queue);
                    data.putString("assay", assay);
                    Message msg = Message.obtain();
                    msg.what = 0x123;
                    msg.setData(data);
                    mHandler.sendMessage(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            };

            ErrorListener errorListener = error -> {
                Message msg = Message.obtain();
                msg.what = 0x123;
                mHandler.sendMessage(msg);
            };

            VolleyCallWebService httpService = new VolleyCallWebService(
                    methodName, param, value, listener, errorListener,
                    Priority.NORMAL);
            VolleySingleton volley = VolleySingleton
                    .getVolleyInstance(getApplicationContext());
            volley.addRequest(httpService);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
